﻿' ------------------------------------------------------------------------------
' The LogControl is a UserControl for displaying log messages.
' The messages are displayed in a ListBox. The ListBox has a
' context menu for clearing the control and saving the contents.
'
' This is a simple implementation that does not perform very fast.
' ------------------------------------------------------------------------------
' This file is part of the SQL Server 2008 Technology Demonstration. This
' application was designed and coded by Eric Weitzman, LandTime Inc, for
' Microsoft Corporation. Contact Ed Katibah at Microsoft about this demo.
' Copyright © 2008 Microsoft Corporation. All rights reserved.
' ------------------------------------------------------------------------------

Public Class LogControl
    Inherits System.Windows.Forms.UserControl

    Private m_log As Boolean = True
    Private m_update_frequently As Boolean = False

#Region " properties "
    Public Property Log() As Boolean
        Get
            Return m_log
        End Get
        Set(ByVal value As Boolean)
            If m_log = value Then Return
            m_log = True
            If value Then
                WriteLine("logging turned on")
            Else
                WriteLine("logging turned off")
            End If
            m_log = value
            lbxLog.Enabled = m_log
        End Set
    End Property
#End Region

    Public WriteOnly Property UpdateFrequently() As Boolean
        Set(ByVal value As Boolean)
            m_update_frequently = value
        End Set
    End Property

    Private Delegate Sub DelegateWriteline(ByVal message As String)

    Public Sub WriteLine(ByVal message As String)
        If Not m_log Then Return

        If InvokeRequired Then
            BeginInvoke(New DelegateWriteline(AddressOf WriteLine), message)
            Return
        End If

        If message.Length > 300 Then message = message.Substring(0, 299) & " (truncated...)"

        lbxLog.BeginUpdate()
        If message.ToLower.StartsWith("exception") Or message.ToLower.StartsWith("sqlexception") Or _
           message.ToLower.StartsWith("assert") Or message.ToLower.StartsWith("error") Then
            message = "******* " & message
        End If
        lbxLog.Items.Add(message)
        lbxLog.SelectedIndex = lbxLog.Items.Count - 1
        lbxLog.EndUpdate()
        If m_update_frequently Then My.Application.DoEvents()
    End Sub

    Private Sub ClearToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ClearToolStripMenuItem.Click
        lbxLog.Items.Clear()
    End Sub

    Private Sub SaveToolStripMenuItem_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveToolStripMenuItem.Click
        Dim d As SaveFileDialog = SaveFileDialog1
        d.CheckPathExists = True
        d.DefaultExt = ".log"
        d.RestoreDirectory = True
        Dim rc As DialogResult = d.ShowDialog()
        If rc = DialogResult.OK Then SaveLog(d.FileName)
    End Sub

    Private Sub SaveLog(ByVal fname As String)
        Dim ostr As New IO.StreamWriter(fname, False)
        For Each item As Object In lbxLog.Items
            ostr.WriteLine(item.ToString())
        Next
        ostr.Close()
        MessageBox.Show("Saved log to file " & fname)
    End Sub

End Class
